package com.jz.yanshe.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.util.PreferencesHelper;

import com.jz.yanshe.R;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.ShouyiResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/4/4.
 */

public class ShouyiDetailActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    @BindView(R.id.tv_title)
            TextView tv_title;
    MyAdapter myAdapter = new MyAdapter();
    List<ShouyiResponse.Shouyi> list = new ArrayList<>();
    int page = 1;
    String userid;
    String date;

    @Override
    protected void initViews() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 2;
            }
        });
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        userid = getIntent().getStringExtra("userid");
        date = getIntent().getStringExtra("date");
        getShouyiDetail(page+"","10");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_shouyi_detail;
    }

    @Override
    public void onLoadMore() {
            page++;
        getShouyiDetail(page + "", "10");
    }

    @Override
    public void onRefresh() {
            page = 1;
        getShouyiDetail(page+"","10");
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Holder(getLayoutInflater().inflate(R.layout.item_shouyi, parent, false));
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            holder.tv_nickname.setText(list.get(position).branchNick);
            holder.tv_order_price.setText(list.get(position).total_order_amount);
            holder.tv_team_type.setText(list.get(position).fee_splitting_type_string);
            holder.tv_my_shouyi.setText(list.get(position).fee_splitting_amount);
        }


        @Override
        public int getItemCount() {
            return list.size();
        }


        class Holder extends RecyclerView.ViewHolder {
            TextView tv_nickname, tv_order_price, tv_team_type,tv_my_shouyi;

            public Holder(View itemView) {
                super(itemView);
                tv_nickname = itemView.findViewById(R.id.tv_nickname);
                tv_order_price = itemView.findViewById(R.id.tv_order_price);
                tv_team_type = itemView.findViewById(R.id.tv_team_type);
                tv_my_shouyi = itemView.findViewById(R.id.tv_my_shouyi);
            }
        }
    }


    private void getShouyiDetail(String page, String pagesize) {
        RestProxy.getInstance().getShouyiDetail(PreferencesHelper.getInstance().getString(this, "token"),userid,date, page, pagesize, new Observer<ShouyiResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ShouyiResponse shouyiResponse) {
                if (ShouyiDetailActivity.this.page == 1) {
                    list.clear();
                }
                list.addAll(shouyiResponse.list);
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
    }

}
